package com.iti.jets.gpts.home;

// default package
// Generated Mar 22, 2014 9:41:00 PM by Hibernate Tools 3.4.0.CR1
import com.iti.jets.gpts.dao.AbstractDAO;
import com.iti.jets.gpts.pojo.Point;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

/**
 * Home object for domain model class Point.
 *
 * @see .Point
 * @author Hibernate Tools
 */
public class PointHome extends AbstractDAO<Point> {

    public PointHome(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    public Point pointcreate(Point object) {
        session.beginTransaction();
//        Query createSQLQuery = session.createSQLQuery("insert into main.point (location, is_stop, name, id) values (\':location\', :stop, :name, :id)")
//                .setParameter("location",object.getLocation())
//                .setBoolean("stop", object.getIsStop())
//                .setString("name", object.getName())
//                .setInteger("id", getNextKey());
        int nextKey = getNextKey();
        Query createSQLQuery = session.createSQLQuery("insert into main.point (location, is_stop, name, id) values ('" + object.getLocation() + "','" + object.getIsStop() + "', '" + object.getIsStop() + "', " + nextKey + ")");
        createSQLQuery.executeUpdate();

        session.getTransaction().commit();
        //object.setId(nextKey);
        Point read = read(nextKey);
        return read;
    }

    public int getNextKey() {
        Query query = session.createSQLQuery("select nextval('point_id_seq')");
        int key = (int) ((BigInteger) query.uniqueResult()).longValue();
        return key;
    }

    public List findByName(String name) {
        List<Point> points = session.createCriteria("com.iti.jets.gpts.pojo.Point")
                .add(Restrictions.ilike("name", "%" + name + "%")).list();
        return points;
    }

    public Point findByExactName(String point) {
        return (Point) session.createCriteria("com.iti.jets.gpts.pojo.Point")
                .add(Restrictions.eq("name", point)).uniqueResult();
    }
    
     public static void main(String[] args)
    {
        SessionFactory sessionFactory = new Configuration().configure("com/iti/jets/gpts/config/hibernate.cfg.xml").buildSessionFactory();
        PointHome g = new PointHome(sessionFactory);
        
        System.out.println(g.findByExactName("Smart"));
    }
    
}
